package gen_b2g.game;

import java.io.*;
import java.util.*;

import com.bowlong.io.*;
import com.bowlong.util.*;
import com.bowlong.net.*;

import gen_b2g.game.bean.*;

@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
public abstract class GameServiceI extends com.bowlong.net.proto.N3Support {

public abstract TcpChannel chn(int XID) throws Exception;


    public static final Set<Integer> CMD = NewSet.create(103149417,3441010,-1538971451,1193247675);
    public static final Map<Integer, String> NAMES = NewMap.create().put2(103149417, "login").put2(3441010, "ping").put2(-1538971451, "submitNewTaskIndex").put2(1193247675, "setNameAndPass");
    public static final Set<Integer> WRITES = NewSet.create(103149417);

    public String getMethodName(int cmd) {
        return NAMES.get(cmd);
    }

    public boolean isWriteMethod(int cmd) {
        return WRITES.contains(cmd);
    }

    public boolean in(NewMap map) throws Exception {
        int cmd = map.getInt(0);
        return CMD.contains(cmd);
    }

    public boolean in(int cmd) throws Exception {
        return CMD.contains(cmd);
    }

    // //////////////////////////////////////////////
    // 逻辑分发
    // //////////////////////////////////////////////

    public int disp(TcpChannel chn, NewMap map) throws Exception {
        if(chn == null) return 0;
        int cmd = map.getInt(0);
        return disp(chn, cmd, map);
    }
    public int disp(TcpChannel chn, int cmd, NewMap map) throws Exception {
        if(chn == null) return 0;
        switch (cmd) {
            case 103149417: { //  登陆
                return __login(chn, map);
                // return "login";
            }
            case 3441010: { //  ping
                return __ping(chn, map);
                // return "ping";
            }
            case -1538971451: { //  提交新手任务步奏
                return __submitNewTaskIndex(chn, map);
                // return "submitNewTaskIndex";
            }
            case 1193247675: { //  设置昵称密码
                return __setNameAndPass(chn, map);
                // return "setNameAndPass";
            }
        }
        throw new Exception(" cmd: " + cmd + ":" + map + " not found processor.");
    }

    // //////////////////////////////////////////////
    // 解析参数
    // //////////////////////////////////////////////

    // 登陆
    private int __login(TcpChannel chn, NewMap map2) throws Exception {
        if(chn == null) return 0;
        String token = map2.getString(110541305);
        String device = map2.getString(-1335157162);
        String deviceSystem = map2.getString(-923409371);
        String deviceCode = map2.getString(780674403);
        String resolution = map2.getString(-1600030548);
        String language = map2.getString(-1613589672);
        String uuid = map2.getString(3601339);
        String pass = map2.getString(3433489);
        String version = map2.getString(351608024);
        String channel = map2.getString(738950403);
        int serverId = map2.getInt(1379103678);
        int loginType = map2.getInt(-1719265725);
        NUser nUser = new NUser();

        ReturnStatus rst = new ReturnStatus();
        try {
            onLogin(chn, token, device, deviceSystem, deviceCode, resolution, language, uuid, pass, version, channel, serverId, loginType, nUser, rst);
        } catch ( Exception e ) {
            Object[] othrows = {"\"token:\"", token, "\"device:\"", device, "\"deviceSystem:\"", deviceSystem, "\"deviceCode:\"", deviceCode, "\"resolution:\"", resolution, "\"language:\"", language, "\"uuid:\"", uuid, "\"pass:\"", pass, "\"version:\"", version, "\"channel:\"", channel, "\"serverId:\"", serverId, "\"loginType:\"", loginType, "\"nUser:\"", nUser, };
            onExcept(chn, "login", Log.rethrow2(e, "login", othrows), rst);
        }
        try ( ByteOutStream result = getStream();) {
            writeMapTag(result, 3);
            writeMapEntry(result, 0, 103149417);
            writeMapEntry(result, 1, rst.toMap());
            writeMapEntry(result, 104233305, nUser.toMap());
            return chn.send(result.toByteArray());
        } catch (Exception e) {
            throw e;
        }
    }

    // ping
    private int __ping(TcpChannel chn, NewMap map2) throws Exception {
        if(chn == null) return 0;
        String str = map2.getString(114225);
        NStr rst2 = new NStr();

        ReturnStatus rst = new ReturnStatus();
        try {
            onPing(chn, str, rst2, rst);
        } catch ( Exception e ) {
            Object[] othrows = {"\"str:\"", str, "\"rst2:\"", rst2, };
            onExcept(chn, "ping", Log.rethrow2(e, "ping", othrows), rst);
        }
        try ( ByteOutStream result = getStream();) {
            writeMapTag(result, 3);
            writeMapEntry(result, 0, 3441010);
            writeMapEntry(result, 1, rst.toMap());
            writeMapEntry(result, 3510335, rst2.toMap());
            return chn.send(result.toByteArray());
        } catch (Exception e) {
            throw e;
        }
    }

    // 提交新手任务步奏
    private int __submitNewTaskIndex(TcpChannel chn, NewMap map2) throws Exception {
        if(chn == null) return 0;
        int index = map2.getInt(100346066);

        ReturnStatus rst = new ReturnStatus();
        try {
            onSubmitNewTaskIndex(chn, index, rst);
        } catch ( Exception e ) {
            Object[] othrows = {"\"index:\"", index, };
            onExcept(chn, "submitNewTaskIndex", Log.rethrow2(e, "submitNewTaskIndex", othrows), rst);
        }
        try ( ByteOutStream result = getStream();) {
            writeMapTag(result, 2);
            writeMapEntry(result, 0, -1538971451);
            writeMapEntry(result, 1, rst.toMap());
            return chn.send(result.toByteArray());
        } catch (Exception e) {
            throw e;
        }
    }

    // 设置昵称密码
    private int __setNameAndPass(TcpChannel chn, NewMap map2) throws Exception {
        if(chn == null) return 0;
        String name = map2.getString(3373707);
        String pass = map2.getString(3433489);

        ReturnStatus rst = new ReturnStatus();
        try {
            onSetNameAndPass(chn, name, pass, rst);
        } catch ( Exception e ) {
            Object[] othrows = {"\"name:\"", name, "\"pass:\"", pass, };
            onExcept(chn, "setNameAndPass", Log.rethrow2(e, "setNameAndPass", othrows), rst);
        }
        try ( ByteOutStream result = getStream();) {
            writeMapTag(result, 2);
            writeMapEntry(result, 0, 1193247675);
            writeMapEntry(result, 1, rst.toMap());
            return chn.send(result.toByteArray());
        } catch (Exception e) {
            throw e;
        }
    }

    // //////////////////////////////////////////////
    // 逻辑分发
    // //////////////////////////////////////////////

    public String _params(NewMap map) throws Exception {
        int cmd = map.getInt(0);
        return _params(cmd, map);
    }
    public String _params(int cmd, NewMap map) throws Exception {
        switch (cmd) {
            case 103149417: { //  登陆
                return __login_params(map);
            }
            case 3441010: { //  ping
                return __ping_params(map);
            }
            case -1538971451: { //  提交新手任务步奏
                return __submitNewTaskIndex_params(map);
            }
            case 1193247675: { //  设置昵称密码
                return __setNameAndPass_params(map);
            }
        }
        throw new Exception(" cmd: " + cmd + ":" + map + " not found processor.");
    }

    // //////////////////////////////////////////////
    // 解析参数 X
    // //////////////////////////////////////////////

    // 登陆
    private static String __login_params(NewMap map2) throws Exception {
        String token = map2.getString(110541305);
        String device = map2.getString(-1335157162);
        String deviceSystem = map2.getString(-923409371);
        String deviceCode = map2.getString(780674403);
        String resolution = map2.getString(-1600030548);
        String language = map2.getString(-1613589672);
        String uuid = map2.getString(3601339);
        String pass = map2.getString(3433489);
        String version = map2.getString(351608024);
        String channel = map2.getString(738950403);
        int serverId = map2.getInt(1379103678);
        int loginType = map2.getInt(-1719265725);
        StringBuffer sb = com.bowlong.objpool.StringBufPool.borrowObject();
        try {
            sb.append("login(");
            sb.append("\"token\":").append(token).append(",");
            sb.append("\"device\":").append(device).append(",");
            sb.append("\"deviceSystem\":").append(deviceSystem).append(",");
            sb.append("\"deviceCode\":").append(deviceCode).append(",");
            sb.append("\"resolution\":").append(resolution).append(",");
            sb.append("\"language\":").append(language).append(",");
            sb.append("\"uuid\":").append(uuid).append(",");
            sb.append("\"pass\":").append(pass).append(",");
            sb.append("\"version\":").append(version).append(",");
            sb.append("\"channel\":").append(channel).append(",");
            sb.append("\"serverId\":").append(serverId).append(",");
            sb.append("\"loginType\":").append(loginType).append(",");
            sb.append(")");
            return sb.toString();
        } finally {
            com.bowlong.objpool.StringBufPool.returnObject(sb);
        }

    }
    // ping
    private static String __ping_params(NewMap map2) throws Exception {
        String str = map2.getString(114225);
        StringBuffer sb = com.bowlong.objpool.StringBufPool.borrowObject();
        try {
            sb.append("ping(");
            sb.append("\"str\":").append(str).append(",");
            sb.append(")");
            return sb.toString();
        } finally {
            com.bowlong.objpool.StringBufPool.returnObject(sb);
        }

    }
    // 提交新手任务步奏
    private static String __submitNewTaskIndex_params(NewMap map2) throws Exception {
        int index = map2.getInt(100346066);
        StringBuffer sb = com.bowlong.objpool.StringBufPool.borrowObject();
        try {
            sb.append("submitNewTaskIndex(");
            sb.append("\"index\":").append(index).append(",");
            sb.append(")");
            return sb.toString();
        } finally {
            com.bowlong.objpool.StringBufPool.returnObject(sb);
        }

    }
    // 设置昵称密码
    private static String __setNameAndPass_params(NewMap map2) throws Exception {
        String name = map2.getString(3373707);
        String pass = map2.getString(3433489);
        StringBuffer sb = com.bowlong.objpool.StringBufPool.borrowObject();
        try {
            sb.append("setNameAndPass(");
            sb.append("\"name\":").append(name).append(",");
            sb.append("\"pass\":").append(pass).append(",");
            sb.append(")");
            return sb.toString();
        } finally {
            com.bowlong.objpool.StringBufPool.returnObject(sb);
        }

    }

    public static Exception rethrow(Exception cause, String method, Object... params) {
        String causeMessage = cause.getMessage();
        if (causeMessage == null) {
            causeMessage = "";
        }
        StringBuffer msg = new StringBuffer(causeMessage);
        msg.append("\r\n");
        msg.append(method);
        msg.append(" Parameters: ");
        msg.append("\r\n");
        if (params == null) {
            msg.append("[]");
        } else {
            msg.append(Arrays.deepToString(params));
        }
        msg.append("\r\n");
        Exception e = new Exception(msg.toString(), cause);
        return e;
    }


    // //////////////////////////////////////////////
    // 需要实现的接口
    // //////////////////////////////////////////////

    public abstract void onExcept(TcpChannel chn, String method, String except, ReturnStatus ret) throws Exception;
    // 登陆
    public abstract void onLogin(TcpChannel chn , String token, String device, String deviceSystem, String deviceCode, String resolution, String language, String uuid, String pass, String version, String channel, int serverId, int loginType, NUser nUser, ReturnStatus ret) throws Exception;
    // ping
    public abstract void onPing(TcpChannel chn , String str, NStr rst2, ReturnStatus ret) throws Exception;
    // 提交新手任务步奏
    public abstract void onSubmitNewTaskIndex(TcpChannel chn , int index, ReturnStatus ret) throws Exception;
    // 设置昵称密码
    public abstract void onSetNameAndPass(TcpChannel chn , String name, String pass, ReturnStatus ret) throws Exception;
}
